package ogmios.dialogs;
import ogmios.core.*;
import ogmios.components.*;
import ogmios.components.munches.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

/**
 * A GUI for browsing through Commands and reading their man pages.
 */ 
public class CommandBrowser extends JFrame
{
 private final JTextArea output;
 private final JComboBox commandList;
 public CommandBrowser()
 {
   setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  setTitle("Command Browser"); 
  output = new JTextArea();
  output.setEditable(false);
   commandList = new JComboBox();
   commandList.addItemListener(
    new ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
         String commandName;
         Command command = CommandManager.get(commandName = (String)commandList.getSelectedObjects()[0]);
         output.setText(command == null ? commandName + " appears to not be a real command...I'm...I'm sorry?\n\nThe Fogmioszy Team :-(" : command.getManPage());
         output.select(0,0);
        }
         else
         {
           output.setText("");
         }
      }
    }
    );
   for (String commandName : new TreeSet<String>(CommandManager.getCommandNames()))
   {
     commandList.addItem(commandName);
   }
   Container contents = getContentPane();
   contents.add(commandList, BorderLayout.NORTH);
   contents.add(new JScrollPane(output));
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   screenSize.width /= 3.8;
   screenSize.height /= 2;
   setSize (screenSize);
   OgmiosUtilities.closeableChild(getRootPane());
 }

}